package com.domain.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 非空判断类
 */
public class NotNullUtils {
    /**
     * 判断对象是否为空（list，对象，字符串）
     *
     * @return true：不为空，false：空
     */
    public static Boolean isNotNullObject(Object object) {
        if (object == null) {
            return false;
        }
        if (object == "") {
            return false;
        }
        if (object instanceof String && StringUtils.isBlank(object.toString())) {
            return false;
        }
        if (object instanceof List || object instanceof ArrayList || object instanceof LinkedList) {
            List list = (List) object;
            if (list.size() <= 0) {
                return false;
            }
        }
        if (object instanceof Set || object instanceof HashSet || object instanceof TreeSet) {
            Set set = (Set) object;
            if (set.size() <= 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 分割字符串
     *
     * @param str
     * @return
     */
    public static List<String> sliptList(String str) {
        List<String> stringList = new ArrayList<>();
        if (NotNullUtils.isNotNullObject(str)) {
            String[] strings = str.split(",");
            for (String s : strings) {
                stringList.add(s);
            }
        }
        return stringList;
    }


    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     * @author ：dapeng
     */
    public static boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    public static String stringUncode(String param) {
        if (param != null && !param.trim().equals("")) {
            try {
//                param = param.replaceAll("%(?![0-9a-fA-F]{2})", "%25");  //测试%转义
                param = URLEncoder.encode(param, "utf-8");//转码
                param = URLDecoder.decode(param, "UTF-8");//解码
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return param;
    }


}
